package com.cn.ccp.service.classschedule;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.RowMapper;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ClassScheduleSettingsBean;
import com.cn.ccp.service.BasicService;
import com.cn.ccp.util.Contants;
import com.cn.ccp.util.MsgConstants;

/**
 * 课程表基本信息设置service
 * @author Administrator
 *
 */
public class ClassScheduleSettingService extends BasicService{
	private Log log = LogFactory.getLog(ClassScheduleSettingService.class);
		
	
	/**
	 * 添加上课节数信息
	 * @param bo
	 * @return
	 */
	public boolean save(ClassScheduleSettingsBean bo){
		log.info(bo);
		String sql = "insert into class_schedule_settings(number,class_schedule_type,start_time,end_time) values (?,?,?,?)";
		return  jdbcTemplate.update(sql ,new Object[]{bo.getNumber(),bo.getClassScheduleType(),bo.getStartTime(),bo.getEndTime()}) > 0;
	}
	
	
	public boolean update(ClassScheduleSettingsBean bo){
		log.info(bo);
		String sql = "update class_schedule_settings set number = ?, class_schedule_type = ? ,start_time = ?,end_time = ? where id = ?";
		return jdbcTemplate.update(sql ,new Object[]{bo.getNumber(),bo.getClassScheduleType(),bo.getStartTime(),bo.getEndTime() , bo.getId()}) > 0; 
	}
	
	
	public boolean delete(int id) {
		String sql = "delete from class_schedule_settings where id = ?";
		return jdbcTemplate.update(sql ,new Object[]{id}) > 0;
	}
	
	/**
	 *根据id查询学期设置
	 * @param id
	 * @return
	 */
	public Object findClassScheduleSettingsById(int id) {
		String sql = "SELECT * FROM class_schedule_settings WHERE id = ?";
		return jdbcTemplate.queryForObject(sql, new Object[] { id }, new ClassScheduleSettingsMapper());
	}
	
	public List pageQuery(int start, int pagesize) {
		String sql = "select * from class_schedule_settings limit ?,?";
		return jdbcTemplate.query(sql, new Object[] { start,pagesize},new ClassScheduleSettingsMapper());
	}
	
	public List<ClassScheduleSettingsBean> getSettings(){
		String sql = " select count(number) as number, class_schedule_type from class_schedule_settings group by class_schedule_type";
		return jdbcTemplate.query(sql,new ClassScheduleSettingsMapper());
	}
	
	
	public int getCount() {
		String sql = "select count(1) from class_schedule_settings ";
		return jdbcTemplate.queryForInt(sql);
	}
	
	
	 class ClassScheduleSettingsMapper implements RowMapper {
			@Override
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				ClassScheduleSettingsBean bean = new ClassScheduleSettingsBean();
				bean.setNumber(rs.getInt("number"));
				bean.setClassScheduleType(rs.getString("class_schedule_type"));
				if(bean.getClassScheduleType().equals(String.valueOf(Contants.SCHEDULE_TYPE_MORNING))){
					bean.setClassScheduleTypeName(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_MORNING));
				}else if(bean.getClassScheduleType().equals(String.valueOf(Contants.SCHEDULE_TYPE_FORENOON))){
					bean.setClassScheduleTypeName(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_FORENOON));
				}else if(bean.getClassScheduleType().equals(String.valueOf(Contants.SCHEDULE_TYPE_AFTERNOON))){
					bean.setClassScheduleTypeName(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_AFTERNOON));
				}else if(bean.getClassScheduleType().equals(String.valueOf(Contants.SCHEDULE_TYPE_NIGHT))){
					bean.setClassScheduleTypeName(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TIME_TYPE_NIGHT));
				}
				try{
					bean.setId(rs.getInt("id"));
					bean.setStartTime(rs.getString("start_time"));
					bean.setEndTime(rs.getString("end_time"));
				}catch(Exception e){};
				return bean;
			}
		}
	
}
